package longrise.phone.com.bjjt_jyb.bindinginformation;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_jyb.Application.BaseApplication;
import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.ApiImpI;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.DialogUtils;
import longrise.phone.com.bjjt_jyb.Utils.PrefUtils;
import longrise.phone.com.bjjt_jyb.myaccident.PhotoImageView;


/**
 * 我的驾驶证界面
 */
public class MyDriveCardView extends LFView implements View.OnClickListener, ILSMsgListener {
    private Context context = null;
    private View view = null;
    private TextView nameET;
    private TextView identityNumET;
    private TextView fileNumET;
    private ImageView driveCardIV;//驾驶证图片
    private LinearLayout modifyLayout;//修改驾驶证

    private EntityBean bean;//数据参数
    private BaseApplication application;

    public MyDriveCardView(Context c, EntityBean bean) {
        super(c);
        this.context = c;
        this.bean = bean;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public void refresh() {

    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        initView();
        regEvent(true);
        setData();
    }

    private void initView() {
        view = View.inflate(context, R.layout.view_mydrivecard, null);
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("我的驾驶证");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnDestroy();
                closeForm();
            }
        });
        nameET = (TextView) view.findViewById(R.id.mycard_ev_name);
        identityNumET = (TextView) view.findViewById(R.id.mycard_ev_identitynum);
        fileNumET = (TextView) view.findViewById(R.id.mycard_ev_filenum);
        driveCardIV = (ImageView) view.findViewById(R.id.mycard_iv_drivecard);
        modifyLayout = (LinearLayout) view.findViewById(R.id.mycard_layout_modify);

        application = (BaseApplication) context.getApplicationContext();
        this.addILSMsgListener(this);
    }

    private void regEvent(boolean b) {
        if (null != modifyLayout) {
            modifyLayout.setOnClickListener(b ? this : null);
        }
        if (null != driveCardIV) {
            driveCardIV.setOnClickListener(b ? this : null);
        }
    }

    //设置数据
    public void setData() {
        if (null == bean) {
            return;
        }
        //姓名
        String name = bean.getString("carownername");
        if (TextUtils.isEmpty(name)) {
            name = "";
        }
        nameET.setText(name);
        //身份证号
        String identity = bean.getString("cardno");
        if (TextUtils.isEmpty(identity)) {
            identity = "";
        }
        identityNumET.setText(identity);
        //档案编号
        String fileno = bean.getString("driverfileno");
        if (TextUtils.isEmpty(fileno)) {
            fileno = "";
        }
        fileNumET.setText(fileno);
        //驾驶证照片
        String pic = bean.getString("drivernoimg");
        if (!TextUtils.isEmpty(pic)) {
            ImageLoader.getInstance().displayImage(pic, driveCardIV, CommontUtils.getOptions());
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.mycard_layout_modify://修改驾驶证信息
                    BindDriveCardView cardView = new BindDriveCardView(context, bean);
                    showForm(cardView);
                    break;
                case R.id.mycard_iv_drivecard://查看大图
                    List<String> PicLists = new ArrayList<String>();
                    PicLists.add(bean.getString("drivernoimg"));
                    PhotoImageView view = new PhotoImageView(context, PicLists);
                    FrameworkManager.getInstance().showNewForm(context, view);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取驾驶证信息
     */
    private void getDriveCard() {
        String userflag = application.getKckpName();
        String token = application.getToken();
        String owner = application.getLoginInfo().getBean("userinfo").getString("name");
        DialogUtils.showDialog(context);
        ApiImpI.getInstance(application).getCarOwner(owner, userflag, token,
                new ApiImpI.RequestCallBack() {
                    @Override
                    public void onError(String error) {
                        DialogUtils.closeDialog();
                    }

                    @Override
                    public void onSuccess(Object result) {
                        DialogUtils.closeDialog();
                        EntityBean beans = (EntityBean) result;
                        String restate = beans.getString("restate");
                        if (TextUtils.equals(restate, "1")) {
                            bean = beans.getBean("data");
                            setData();
                        }
                    }
                });
    }

    @Override
    public Object onLSMsg(int i, Object... objects) {
//        if (i == 99) {//驾驶证数据发生变化
//            getDriveCard();
//        }
        if (i == 101) {
            EntityBean resultBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(PrefUtils.
                    getString(context, "carinfo", null), EntityBean.class);
            EntityBean dataBean = resultBean.getBean("data");
            if (null != dataBean) {
                bean = dataBean.getBean("use");
                setData();
            }
        }
        return null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        this.removeILSMsgListener(this);
        regEvent(false);
        DialogUtils.destroyDialog();
    }
}